jQuery(document).on("pagebeforeshow", "[data-role='page'][data-pagemenu='true']", function(){
    var page = jQuery(this);
    var panel = page.find("[data-role='panel']");
    
    // if click the .showMenu button, open the menu
    page.find("a.showMenu").click(function(){
        panel.panel("open");       
    });
    

    panel.find("a.menu-item-login").click(function() {
         $.ajax({
            type: "POST",
            url: URL_ACTION_CHECK,
            data: {},

            success: function(msg) {
                $.mobile.changePage(URL_VIEW_SERVICES, { transition: "slide", reverse: false, changeHash: true });
            },

            error: function(req, status, error) {
                $.mobile.changePage(URL_VIEW_LOGIN, { transition: "slide", reverse: false, changeHash: true });
            }
        });
    }).end().find("a.menu-item-logout").click(function() {
        $.ajax({
            type: "POST",
            url: URL_ACTION_LOGOUT,
            data: {},

            success: function(msg) {
                $.cookie("wia_email", "");
                $.cookie("wia_password", "");
                panel.panel("close");
                $.mobile.changePage(URL_VIEW_LOGIN, { transition: "slide", reverse: false, changeHash: true });
            },

            error: function(req, status, error) {
                panel.panel("close");
                var errmsg = page.find("div[data-role='popup']");
                if (errmsg.length) {
                    errmsg.find("p").html("您还没有登录！");
                    errmsg.popup("open");
                }
            }
        });
    });
});

